
#include "main.h"


CLOCK_t clock;

iram SYSTEM_TMR_t system_tmrs;
iram CYCLE_TMR_t cycle_tmr;

//iram bool base_5ms_f;
bool base_10ms_f;
bool half_sec_f;


void delay_10us(u16 i)
{
	while(i--);
}

void Clock(CLOCK_t * clock)
{	
	if (system_sec_f)
	{//1S
		if (++clock->second >= 60)
		{
			clock->second = 0;
            
			if (++clock->minute >= 60)
			{
				clock->minute = 0;			//1分钟到

				if (++clock->hour >= 24)
				{
					clock->hour = 0;
				}
			}
		}
	}
}


void clock_init(CLOCK_t * clock)
{
	clock->hour   = 0;
	clock->minute = 0;
	clock->second = 0;
}


void TimerNms_count(void)
{
	static u8 nms_ct = 0;
    static u8 ns30_ct = 0;
    static u8 nstime_ct = 0;
    static u8 ms600_ct = 0;
//	if (++cycle_tmr.tmr >= cycle_tmr.width) cycle_tmr.tmr = 0;

	system_tmrs.timer_flags_byte = 0;
    
	if (++nms_ct >= 100)
		nms_ct = 0;
	
	if (nms_ct & 1)							
		system_20ms_f = true;
	if ((nms_ct % 5) == 0)
		system_50ms_f = true;
	if ((nms_ct % 10) == 0)	
    {
		system_100ms_f = true;
        if(++ms600_ct >= 6)
        {
            ms600_ct = 0;
            system_600ms_f = 1;
        }
		if(steamCtrl.state == STA_WORK)
		{
            u8_dn(&gu8_AddWaterTime1);  //加水倒计时
            u8_dn(&gu8_AddWaterTime2);
		}
    }
    if((nms_ct % 20) == 0)
    {
        system_200ms_f = true;
        if(gb_tmconnet && (gu8_version == 1)&&(gb_Sendtianmao == 0))
        {
            gb_tmconnet = 0;
            send_tianmaonet_comm();
        }
        else if(gb_tmrest && (gu8_version == 1)&&(gb_Sendtianmao == 0))
        {
            gb_tmrest = 0;
            send_tianmaorest_comm();
        }        
    }
	if ((nms_ct == 0) || (nms_ct == 50))	
	{
		system_500ms_f = true;      
		half_sec_f ^= 1;
		
	}
	if (nms_ct == 0) 
	{
		system_sec_f = true; 
        // if(start_buzzcnt)
        // {
        //     start_buzzcnt--;
        //     buzz_start(BUZ_KEY);
        // }
        if(worktime)
        {
            worktime--;               
        }
        if(gu8_SystemBeginTime)
        {
            gu8_SystemBeginTime--;
        }
        if((steamCtrl.state == STA_NULL)||(steamCtrl.state == STA_APPOINT))
            u8_dn(&gu8_FAdWdelay);
        ns30_ct++;
        if(ns30_ct == 10)
        {
            ns30_ct = 0;
            STEAM_PID.run_f = 1;
            BAKE_PID.run_f = 1;
            

        }           
        nstime_ct++;
        if(nstime_ct >= 60)
        {
            nstime_ct = 0;
        
//            if(board.cmd == 0)
//                board.cmd = 0x01;
//            else
//                board.cmd = 0;   

        }
	}
}














